#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>

using namespace std;


class Solution {
public:
    void duplicateZeros(vector<int>& arr)
    {
        int cur = 0;
        int dest = -1;
        int n = arr.size();
        for (cur = 0; cur < n; cur++)
        {
            if (arr[cur])
            {
                dest++;
            }
            else
            {
                dest += 2;
            }
            if (dest >= n - 1)
            {
                break;
            }
        }
        if (dest == n)
        {
            arr[n - 1] = 0;
            cur--;
            dest -= 2;
        }
        for (; cur >= 0; cur--)
        {
            if (arr[cur])
            {
                arr[dest--] = arr[cur];
            }
            else
            {
                arr[dest--] = arr[cur];
                arr[dest--] = arr[cur];
            }
        }

    }
};

int main()
{
	vector<int> v = { 1,0,2,3,0,4,5,0 };
    Solution s1;
    s1.duplicateZeros(v);
    
}